package com.school.serviceImpl;

import com.school.dao.UserMapper;
import com.school.dto.user.LoginDTO;
import com.school.dto.user.UserArrayDTO;
import com.school.model.PageModel;
import com.school.model.Score;
import com.school.model.User;
import com.school.service.DepartmentService;
import com.school.service.ScoreService;
import com.school.service.UserService;
import com.school.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private DepartmentService departmentService;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        User user = new User();
        user.setId(id);
        user.setIsDelete(2);
        user.setModifyTime(new Date());
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public synchronized int insertSelective(User record) {
        if (userMapper.selectByUserName(record.getUserName()) != null) {
            return -1;
        }
        if (record.getRole() == 1 || record.getRole() == 2 || record.getRole() == 3) {
            record.setDepartmentName(departmentService.selectByPrimaryKey(record.getDepartmentId()).getName());
        }
        if (record.getRole() == 1) {
            if (record.getClassTeacherId() == null) {
                return -2;
            }
        }
        record.setCreateTime(new Date());
        record.setIsDelete(1);
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageModel selectArrayByParam(UserArrayDTO dto) {
        PageModel model = new PageModel();
        int count = userMapper.count(dto);
        int totalPage = 0;
        if (count % dto.getPageSize() > 0) {
            totalPage = count / dto.getPageSize() + 1;
        } else {
            totalPage = count / dto.getPageSize();
        }
        model.setTotalPage(totalPage);
        model.setPageSize(dto.getPageSize());
        model.setPageNum(dto.getPageNum());
        model.setStartNum((dto.getPageNum() - 1) * dto.getPageSize());
        dto.setStartNum(model.getStartNum());
        model.setTotalData(count);
        List<UserInfoVO> list = userMapper.selectArrayByParam(dto);
        if (dto.getRole() != null && dto.getRole() == 2 && list.size() > 0) {//判断查看教师列表，则给出教师的分数
            List<Integer> idList = new ArrayList<>();
            for (UserInfoVO now : list) {
                idList.add(now.getId());
            }
            Map<Integer, List<Score>> map = scoreService.selectAVGBytTIds(idList);
            for (UserInfoVO now : list) {
                List<Score> scores = map.get(now.getId());
                for (Score now2 : scores) {
                    switch (now2.getCommentatorRole()) {
                        case 1:
                            now.setStudentScore(now2.getScore());
                            break;
                        case 2:
                            now.setTeacherScore(now2.getScore());
                            break;
                        case 3:
                            now.setExpertScore(now2.getScore());
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        model.setObj(list);
        return model;
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        User user=selectByPrimaryKey(record.getId());
        if (record.getDepartmentId() != null) {
            record.setDepartmentName(departmentService.selectByPrimaryKey(record.getDepartmentId()).getName());
        }
        if (user.getRole() == 1&&record.getDepartmentId()!=null) {
            if (StringUtils.isEmpty(record.getClassTeacherId())) {
                return -2;
            }
        }
        record.setModifyTime(new Date());
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public User login(LoginDTO dto) {
        return userMapper.login(dto);
    }

    @Override
    public int updateDepartmentNameByDepId(Integer dId, String dName) {
        return userMapper.updateDepartmentNameByDepId(dId, dName);
    }

    @Override
    public int updateByDep(User user) {
        return userMapper.updateByDep(user);
    }
}
